<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退货申请时间" prop="returnApplyTime">
        <el-date-picker clearable
          v-model="queryParams.returnApplyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择退货申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单号" prop="returnOrderId">
        <el-input
          v-model="queryParams.returnOrderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货状态" prop="returnState">
        <el-input
          v-model="queryParams.returnState"
          placeholder="请输入退货状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户账号" prop="returnUerId">
        <el-input
          v-model="queryParams.returnUerId"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="returnPrice">
        <el-input
          v-model="queryParams.returnPrice"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作员" prop="returnOperatorName">
        <el-input
          v-model="queryParams.returnOperatorName"
          placeholder="请输入操作员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作员账号" prop="returnOperatorNumber" v-show="false">
        <el-input
          v-model="queryParams.returnOperatorNumber"
          placeholder="请输入操作员账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作时间" prop="returnDealTime">
        <el-date-picker clearable
          v-model="queryParams.returnDealTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择操作时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退货原因Id" prop="returnReasonId">
        <el-input
          v-model="queryParams.returnReasonId"
          placeholder="请输入退货原因Id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" >
      <el-col :span="1.5" v-show="false">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['orderReturn:orderReturn:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['orderReturn:orderReturn:edit']"
        >审核审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['orderReturn:orderReturn:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['orderReturn:orderReturn:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderReturnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="退货唯一标识" align="center" prop="returnId" v-show="false" />-->
      <el-table-column label="退货申请时间" align="center" prop="returnApplyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnApplyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="returnOrderId" />
      <el-table-column label="退货状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.returnState === 1" >同意</span>
          <span v-if="scope.row.returnState ===2">拒绝</span>
          <span v-if="scope.row.returnState ===3">审核中</span>
        </template>
      </el-table-column>

      <el-table-column label="用户账号" align="center" prop="user.usrName" />
      <el-table-column label="退款金额" align="center" prop="returnPrice" />
      <el-table-column label="操作员" align="center" prop="returnOperatorName" />
<!--      <el-table-column label="操作员账号" align="center" prop="returnOperatorNumber" v-show="false" />-->
      <el-table-column label="操作时间" align="center" prop="returnDealTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnDealTime, '{y}-{m}-{d} {hh}:{mm}:{}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货原因" align="center" prop="returnReason.reasonText" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['orderReturn:orderReturn:edit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['orderReturn:orderReturn:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或审核商品退货对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退货申请时间" prop="returnApplyTime" v-show="false">
          <el-date-picker clearable
            v-model="form.returnApplyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退货申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单号" prop="returnOrderId" >
          <el-input v-model="form.returnOrderId" placeholder="请输入订单号" :readonly="true"/>
        </el-form-item>
        <el-form-item label="用户账号" prop="returnUerId" >
          <el-input v-model="form.user.usrName" placeholder="请输入用户账号" :readonly="true"/>
        </el-form-item>
        <el-form-item label="退货原因" prop="returnReasonId">
          <el-input v-model="form.returnReason.reasonText" :readonly="true" />
        </el-form-item>
<!--          需要修改的地方，显示名称而不是显示数字-->
          <el-form-item label="退货状态" prop="returnState">
              <el-select  v-model="form.returnState"   placeholder="请选择是否同意">
                <el-option label="审核中" value="3"></el-option>
                <el-option label="同意" value="1"></el-option>
                <el-option label="拒绝" value="2"></el-option>
              </el-select>
            <el-input v-if="form.returnState === 1" :readonly="true" value="已同意退款"></el-input>
            <el-input v-if="form.returnState === 2" :readonly="true" value="已拒绝退款"></el-input>
            <el-input v-if="form.returnState === 3" :readonly="true" value="正在审核中"></el-input>
          </el-form-item>
        <el-form-item label="退款金额" prop="returnPrice" >
          <el-input v-model="form.returnPrice" placeholder="请输入退款金额" :readonly="true"/>
        </el-form-item>
        <el-form-item label="操作员" prop="returnOperatorName" v-show="false">
          <el-input v-model="form.returnOperatorName" placeholder="请输入操作员" />
        </el-form-item>
        <el-form-item label="操作员账号" prop="returnOperatorNumber" v-show="false">
          <el-input v-model="form.returnOperatorNumber" placeholder="请输入操作员账号" />
        </el-form-item>
        <el-form-item label="操作时间" prop="returnDealTime" v-show="false">
          <el-date-picker clearable
            v-model="form.returnDealTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderReturn, getOrderReturn, delOrderReturn, addOrderReturn, updateOrderReturn } from "@/api/orderReturn/orderReturn";

export default {
  name: "OrderReturn",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品退货表格数据
      orderReturnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnApplyTime: null,
        returnOrderId: null,
        returnState: null,
        returnUerId: null,
        returnPrice: null,
        returnOperatorName: null,
        returnOperatorNumber: null,
        returnDealTime: null,
        returnReasonId: null
      },
      // 表单参数
      form: {
        returnReason:{
          reasonText:null,
        },
        user:{
          usrName:null,
        }
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品退货列表 */
    getList() {
      this.loading = true;
      listOrderReturn(this.queryParams).then(response => {
        this.orderReturnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        returnId: null,
        returnApplyTime: null,
        returnOrderId: null,
        returnState: null,
        returnUerId: null,
        returnPrice: null,
        returnOperatorName: null,
        returnOperatorNumber: null,
        returnDealTime: null,
        returnReasonId: null,
        returnReason:{
          reasonText:null,
        },
        user:{
          usrName: null,
        }
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.returnId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品退货";
    },
    /** 审核按钮操作 */
    handleUpdate(row) {
      this.reset();
      const returnId = row.returnId || this.ids
      getOrderReturn(returnId).then(response => {
        console.log("原因：", response.data)
        this.form = response.data;
        this.open = true;
        this.title = "审核商品退款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.returnId != null) {
            updateOrderReturn(this.form).then(response => {
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderReturn(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const returnIds = row.returnId || this.ids;
      this.$modal.confirm('是否确认删除商品退货编号为"' + returnIds + '"的数据项？').then(function() {
        return delOrderReturn(returnIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('orderReturn/orderReturn/export', {
        ...this.queryParams
      }, `orderReturn_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
